package cgs_worksheet

import (
	"fmt"
	"cgs-server/helper"
	"go.mongodb.org/mongo-driver/bson"
)

// formItem
type FormItem struct {
	Type    string `json:"type"`  // 表单类型
	Label   string `json:"label"` // 标题文字
	Icon    string `json:"icon"`
	Options map[string]interface{} `json:"options"`
	Model   string `json:"model"` // 数据字段
	Key     string `json:"key"`
	Rules   []map[string]interface{} `json:"rules"`
}

func NewFormItem(field *Field) *FormItem {
	if f, ok := FormItemMap[field.DataType.(string)]; ok {
		f.Model = field.Name
		f.Key = field.Name
		f.Label = field.Name
		if field.Alia != "" {
			f.Label = field.Alia
		}
		return &f
	}
	return nil
}

var FormItemMap = map[string]FormItem{
	"input": {
		Type:  "input", // 表单类型
		Label: "输入框",   // 标题文字
		Icon:  "icon-write",
		Options: map[string]interface{}{
			"type":         "text",
			"width":        "100%", // 宽度
			"defaultValue": "",     // 默认值
			"placeholder":  "请输入",  // 没有输入时，提示文字
			"clearable":    false,
			"maxLength":    nil,
			"hidden":       false, // 是否隐藏，false显示，true隐藏
			"disabled":     false, // 是否禁用，false不禁用，true禁用
		},
		Model: "", // 数据字段
		Key:   "",
		Rules: []map[string]interface{}{
			//验证规则
			{
				"required": false, // 必须填写
				"message":  "必填项",
			},
		},
	},
	"number": {
		Type:  "number", // 表单类型
		Label: "数字输入框",  // 标题文字
		Icon:  "icon-number",
		Options: map[string]interface{}{
			"width":        "100%", // 宽度
			"defaultValue": 0,      // 默认值
			"min":          nil,    // 可输入最小值
			"max":          nil,    // 可输入最大值
			"precision":    nil,
			"step":         1,     // 步长，点击加减按钮时候，加减多少
			"hidden":       false, // 是否隐藏，false显示，true隐藏
			"disabled":     false, //是否禁用
			"placeholder":  "请输入",
			"unit":         nil, // 单位
		},
		Model: "", // 数据字段
		Key:   "",
		Rules: []map[string]interface{}{
			//验证规则
			{
				"required": false, // 必须填写
				"message":  "必填项",
			},
		},
	},
}

const config = `{
    "layout": "horizontal",
    "labelCol": {
      "xs": 4,
      "sm": 4,
      "md": 4,
      "lg": 4,
      "xl": 4,
      "xxl": 4
    },
    "wrapperCol": {
      "xs": 18,
      "sm": 18,
      "md": 18,
      "lg": 18,
      "xl": 18,
      "xxl": 18
    },
    "hideRequiredMark": false,
    "customStyle": ""
  }`

type FormDesignData struct {
	List []FormItem `json:"list"`
	Config interface{} `json:"config"`
}

func NewFormDesignData(length int) *FormDesignData {
	configData := bson.M{}
	_ = helper.FromJSON([]byte(config), &configData)
	return &FormDesignData{
		List:   make([]FormItem, length),
		Config: configData,
	}
}

func (f *FormDesignData) ToWorksheetInfo(wsName string) *WorksheetInfo {
	wsInfo := WorksheetInfo{
		WsName: wsName,
		Fields: make([]*Field, len(f.List)),
	}
	for i := range f.List {
		wsInfo.Fields[i] = &Field{
			Name:     f.List[i].Key,
			Alia:     f.List[i].Label,
			DataType: f.List[i].Type,
			Options:  f.List[i].Options,
		}
	}

	json, _ := helper.ToJSON(f)
	wsInfo.Config = string(json)
	// todo wsInfo.VisualData 需要处理

	return &wsInfo
}

type KFormDesignVo struct {
	WsName string `json:"wsName"`
	Fields []*Field `json:"fields"`
}

func (k *KFormDesignVo) ToWorksheetInfo() (*WorksheetInfo, error) {
	fdd := NewFormDesignData(len(k.Fields))

	for i := range k.Fields {
		item := NewFormItem(k.Fields[i])
		if item == nil {
			return nil, fmt.Errorf("Field %s's type is not exists", k.Fields[i].Name)
		}
		fdd.List[i] = *item
	}

	return fdd.ToWorksheetInfo(k.WsName), nil
}



